package com.huawei.hwmdemo.view;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;



import com.huawei.hwmdemo.function.LoginAndJoinSubThread;
import com.huawei.hwmdemo.R;
import com.huawei.hwmdemo.mqtt.IGetMessageCallBack;
import com.huawei.hwmdemo.mqtt.MQTTService;
import com.huawei.hwmdemo.mqtt.MyServiceConnection;

public class DemoActivity extends FragmentActivity {

    private static final int MY_PERMISSIONS_REQUEST_HWM = 1;
    private static String[] PERMISSIONS = {
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };



    String nickName = "myAndroid";
    String account = "18476925171";
    String password = "2262093abc";
    String mId = "938511663";
    String mPassword = "993350";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){

            ActivityCompat.requestPermissions(this, PERMISSIONS, MY_PERMISSIONS_REQUEST_HWM);
        }
        else {
//            LoginAndJoinSubThread loginAndJoinSubThread = new LoginAndJoinSubThread(getApplication(), nickName, account, password, mId, mPassword);
//            new Thread(loginAndJoinSubThread).start();



        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // 为了防止使用第三方采集推流时，Android6.0设备在屏幕旋转时，界面destroy导致悬浮窗的SurfaceViw接收不到摄像头采集的数据
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_HWM: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //授权成功，继续
//            LoginAndJoinSubThread loginAndJoinSubThread = new LoginAndJoinSubThread(getApplication(), nickName, account, password, mId, mPassword);
//            new Thread(loginAndJoinSubThread).start();

                } else {
                    //授权失败
                    Toast.makeText(this, "授权失败！", Toast.LENGTH_LONG).show();
                }
                break;
            }

        }
    }
}
